package br.com.tcc.systemdx.web.editors;

import java.beans.PropertyEditorSupport;

import br.com.tcc.systemdx.domain.ValorSintoma;
import br.com.tcc.systemdx.persistence.ValorSintomaDAO;

public class ValorSintomaPropertyEditor extends PropertyEditorSupport {
	
	private ValorSintomaDAO valorSintomaDAO;
	
	public ValorSintomaPropertyEditor(ValorSintomaDAO valorSintomaDAO) {
		this.valorSintomaDAO = valorSintomaDAO;
	}
	
	@Override
	public String getAsText() {
		ValorSintoma obj = (ValorSintoma) getValue();
		if (obj == null)
			return "";
		else
			return obj.getId().toString();
	}
	
	@Override
	public void setAsText(final String value) {
		try {
			Long id = Long.parseLong(value);
			ValorSintoma valorSintoma = valorSintomaDAO.findById(id);
			if (valorSintoma != null)
				super.setValue(valorSintoma);
			else
				throw new IllegalArgumentException("Binding error. Cannot find userAccount with id  ["+value+"]");
		} catch (NumberFormatException ex) {
			throw new IllegalArgumentException("Binding error. Invalid id: " + value);
		}
		
	}

}
